/*
 * @Copyright: Copyright (c) 2030 吴周海
 * @Author: wzh200x@126.com
 * @Date: 2025-10-21 16:28:25
 * @LastEditors: wzh200x@126.com
 * @LastEditTime: 2025-10-22 15:52:57
 * @FilePath: \gogamewebserver\goGameWebServer\internal\controller\report\report_v1.go
 * @Description: 文件功能描述
 */
package report

import (
	"context"
	"encoding/json"
	v1 "goGameWebServer/api/report/v1"
	"goGameWebServer/internal/common/utils"
	"goGameWebServer/internal/controller"
	"goGameWebServer/internal/defines/cerror"
	"goGameWebServer/internal/defines/entityName"
	"goGameWebServer/internal/defines/platform"
	"goGameWebServer/internal/manager/platformmanager"
	"goGameWebServer/internal/model/entity"
	"goGameWebServer/internal/register"

	"github.com/gogf/gf/v2/frame/g"
)

func (c *ReprotV1ControllerPrivate) ReportAlipay(ctx context.Context, req *v1.ReportAlipayReq) (res *v1.ReportAlipayRes, err error) {
	r := g.RequestFromCtx(ctx)
	// uid := r.GetParam("userID").Uint()
	uName := r.GetParam("username").String()
	// 查询账号
	ac := register.GetModel(entityName.EntityName_Account)
	acD := ac.GetEntityData().(*entity.AccountModelData)
	acD.AccountName = uName
	ac.LoadEntityDataFromDB(nil)
	if req.AccountId != acD.Id {
		controller.BackResponseError(r, cerror.HTTP_ACCOUNT_NOT_FOUND_ERROR, cerror.GetErrMsg(cerror.HTTP_ACCOUNT_NOT_FOUND_ERROR))
		return
	}
	// 上报记录到数据库,方便查询
	alipayReportM := register.GetModel(entityName.EntityName_AlipayReport)
	alipayReportD := alipayReportM.GetEntityData().(*entity.AlipayReportModelData)
	alipayReportD.ThirdAccount = uName                          // open_id
	alipayReportD.ActionCode = req.ActionCode                   // action_code
	alipayReportD.ActionFinishChannel = req.ActionFinishChannel // action_finish_channel
	alipayReportD.OutBizNo = utils.GenerateUUID()               // out_biz_no
	alipayReportM.LoadEntityDataFromDB(nil)
	if alipayReportD.Status == 1 {
		controller.BackResponseError(r, cerror.HTTP_PLATFORM_REPORT_REPEAT_ERROR,
			cerror.GetErrMsg(cerror.HTTP_PLATFORM_REPORT_REPEAT_ERROR))
		return
	}
	params := make(map[string]interface{})
	params["out_biz_no"] = alipayReportD.OutBizNo
	params["open_id"] = uName
	params["action_code"] = req.ActionCode
	params["action_finish_channel"] = req.ActionFinishChannel
	params["action_finish_date"] = utils.GetNowTimeFormat()
	re, err := platformmanager.ManagerPlatform().GetPlatform(platform.AliPayGame).Report(params, platformmanager.ManagerPlatform().GetPlatformOption(req.AppId))
	if err != nil || re["code"] != 0 {
		controller.BackResponseError(r, cerror.HTTP_PLATFORM_REPORT_ERROR, cerror.GetErrMsg(cerror.HTTP_PLATFORM_REPORT_ERROR))
	} else {
		alipayReportD.Status = 1
		alipayReportD.ActionFinishDate = utils.GetNowTimeFormat()
	}
	responseStr, _ := json.Marshal(re["result"])
	alipayReportD.Response = string(responseStr)
	alipayReportM.SaveEntityDataToDB()
	controller.BackResponseOK(r, "success")
	return
}

func (c *ReprotV1ControllerPrivate) ReportByteJump(ctx context.Context, req *v1.ReportByteJumpReq) (res *v1.ReportByteJumpRes, err error) {
	r := g.RequestFromCtx(ctx)
	uName := r.GetParam("username").String()
	// 查询账号
	ac := register.GetModel(entityName.EntityName_Account)
	acD := ac.GetEntityData().(*entity.AccountModelData)
	acD.AccountName = uName
	ac.LoadEntityDataFromDB(nil)
	if req.AccountId != acD.Id {
		controller.BackResponseError(r, cerror.HTTP_ACCOUNT_NOT_FOUND_ERROR, cerror.GetErrMsg(cerror.HTTP_ACCOUNT_NOT_FOUND_ERROR))
		return
	}
	// 上报记录到数据库,方便查询
	reportM := register.GetModel(entityName.EntityName_ByteJumpReport)
	reportD := reportM.GetEntityData().(*entity.ByteJumpReportModelData)
	reportD.ThirdAccount = uName                          // open_id
	reportD.ActionCode = req.ActionCode                   // action_code
	reportD.ActionFinishChannel = req.ActionFinishChannel // action_finish_channel
	reportD.OutBizNo = utils.GenerateUUID()               // out_biz_no
	reportM.LoadEntityDataFromDB(nil)
	if reportD.Status == 1 {
		controller.BackResponseError(r, cerror.HTTP_PLATFORM_REPORT_REPEAT_ERROR,
			cerror.GetErrMsg(cerror.HTTP_PLATFORM_REPORT_REPEAT_ERROR))
		return
	}
	params := make(map[string]interface{})
	params["out_biz_no"] = reportD.OutBizNo
	params["open_id"] = uName
	params["action_code"] = req.ActionCode
	params["action_finish_channel"] = req.ActionFinishChannel
	params["action_finish_date"] = utils.GetNowTimeFormat()
	re, err := platformmanager.ManagerPlatform().GetPlatform(platform.ByteJumpGame).Report(params, platformmanager.ManagerPlatform().GetPlatformOption(req.AppId))
	if err != nil || re["code"] != 0 {
		controller.BackResponseError(r, cerror.HTTP_PLATFORM_REPORT_ERROR, cerror.GetErrMsg(cerror.HTTP_PLATFORM_REPORT_ERROR))
	} else {
		reportD.Status = 1
		reportD.ActionFinishDate = utils.GetNowTimeFormat()
	}
	// reportD.Response = re["result"].(string)
	reportM.SaveEntityDataToDB()
	controller.BackResponseOK(r, "success")
	return
}

func (c *ReprotV1ControllerPrivate) ReportKuaiShou(ctx context.Context, req *v1.ReportKuaiShouReq) (res *v1.ReportKuaiShouRes, err error) {
	r := g.RequestFromCtx(ctx)
	uName := r.GetParam("username").String()
	// 查询账号
	ac := register.GetModel(entityName.EntityName_Account)
	acD := ac.GetEntityData().(*entity.AccountModelData)
	acD.AccountName = uName
	ac.LoadEntityDataFromDB(nil)
	if req.AccountId != acD.Id {
		controller.BackResponseError(r, cerror.HTTP_ACCOUNT_NOT_FOUND_ERROR, cerror.GetErrMsg(cerror.HTTP_ACCOUNT_NOT_FOUND_ERROR))
		return
	}
	// 上报记录到数据库,方便查询
	reportM := register.GetModel(entityName.EntityName_ByteJumpReport)
	reportD := reportM.GetEntityData().(*entity.ByteJumpReportModelData)
	reportD.ThirdAccount = uName                          // open_id
	reportD.ActionCode = req.ActionCode                   // action_code
	reportD.ActionFinishChannel = req.ActionFinishChannel // action_finish_channel
	reportD.OutBizNo = utils.GenerateUUID()               // out_biz_no
	reportM.LoadEntityDataFromDB(nil)
	if reportD.Status == 1 {
		controller.BackResponseError(r, cerror.HTTP_PLATFORM_REPORT_REPEAT_ERROR,
			cerror.GetErrMsg(cerror.HTTP_PLATFORM_REPORT_REPEAT_ERROR))
		return
	}
	params := make(map[string]interface{})
	params["action_code"] = req.ActionCode
	params["action_finish_channel"] = req.ActionFinishChannel
	params["event_props"] = req.EventProps
	re, err := platformmanager.ManagerPlatform().GetPlatform(platform.KuaiShouGame).Report(params, platformmanager.ManagerPlatform().GetPlatformOption(req.AppId))
	if err != nil || re["code"] != 0 {
		controller.BackResponseError(r, cerror.HTTP_PLATFORM_REPORT_ERROR, cerror.GetErrMsg(cerror.HTTP_PLATFORM_REPORT_ERROR))
	} else {
		reportD.Status = 1
		reportD.ActionFinishDate = utils.GetNowTimeFormat()
	}
	reportD.Response = re["result"].(string)
	reportM.SaveEntityDataToDB()
	controller.BackResponseOK(r, "success")
	return
}
